﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Autenticazione
{
    public partial class Atleti : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["EMail"] == null)
                Response.Redirect("Autenticazione.aspx");

            msgLoggedUser.Text = string.Format("Utente corrente: {0}", Session["EMail"].ToString());

            if (!IsPostBack)
                LoadGrid();
        }

        protected string GetGender(object gender)
        {
            int nGender = int.Parse(gender.ToString());
            
            if (nGender == 0)
                return "Maschile";
            else
                return "Feminile";
        }

        protected string GetBeltDescription(object idBelt)
        {
            int id = int.Parse(idBelt.ToString());
            BusinessObject.Cintura c = new BusinessObject.Cintura();

            c.Id = id;
            return c.GetBeltDescription();
        }

        protected bool SelectedGender(string selValue, object gender)
        {
            string sGender = gender.ToString();
            if (selValue == sGender)
                return true;
            else
                return false;
        }

        protected string GetBirthDay(object birthDay)
        {
            DateTime dBirthDay = DateTime.Parse(birthDay.ToString());

            return dBirthDay.ToString("dd/MM/yyyy");
        }

        private void LoadGrid()
        {
            BusinessObject.Atleta a = new BusinessObject.Atleta();
            DataTable dt = null;
            DataTable dtView = null;

            if (Session["EMail"] == null)
                return;

            dt = a.GetTbAtleti(Session["Email"].ToString());

            dtView = dt.Clone();

            for (int i = dtgAtleta.CurrentPageIndex * dtgAtleta.PageSize; i <= (dtgAtleta.CurrentPageIndex + 1) * dtgAtleta.PageSize - 1; i++)
            {
                if (i <= dt.Rows.Count - 1)
                    dtView.ImportRow(dt.Rows[i]);
            }

            dtgAtleta.VirtualItemCount = dt.Rows.Count; ;

            dtgAtleta.DataSource = dtView;
            dtgAtleta.DataBind();
        }

        protected void dtgAtleta_Edit(object sender, DataGridCommandEventArgs e)
        {
            ClearViewState();
            dtgAtleta.EditItemIndex = e.Item.ItemIndex;

            LoadGrid();
        }

        protected void dtgAtleta_Delete(object sender, DataGridCommandEventArgs e)
        {
            BusinessObject.Atleta a = new BusinessObject.Atleta();

            if (Session["EMail"] == null)
                return;

            a.Id = long.Parse(dtgAtleta.DataKeys[e.Item.ItemIndex].ToString());
            a.EMail = Session["EMail"].ToString();

            a.DeleteAtleta();

            dtgAtleta.EditItemIndex = -1;

            LoadGrid();
        }

        protected void dtgAtleta_Cancel(object sender, DataGridCommandEventArgs e)
        {
            dtgAtleta.EditItemIndex = -1;

            LoadGrid();
        }

        protected void dtgAtleta_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {
            string year, month;
            BusinessObject.Cintura belt = null;
            if (e.Item.ItemType == ListItemType.EditItem)
            {
                //manage gender
                DropDownList ddl = e.Item.Cells[1].FindControl("edit_Gender") as DropDownList;
                if (ddl != null)
                {
                    Tools.Tools.FillComboGender(ddl);
                    ddl.SelectedValue = (e.Item.DataItem as DataRowView).Row["Gender"].ToString();
                }

                //manage birth
                DateTime dt = DateTime.Parse((e.Item.DataItem as DataRowView).Row["BirthDay"].ToString());
                if (ViewState["Month"] == null)
                    month = dt.Month.ToString();
                else
                    month = ViewState["Month"].ToString();

                if (ViewState["Year"] == null)
                    year = dt.Year.ToString();
                else
                    year = ViewState["Year"].ToString();

                DropDownList ddlYear = e.Item.Cells[2].FindControl("edit_BirthYear") as DropDownList;
                if (ddlYear != null)
                {
                    Tools.Tools.FillComboYears(ddlYear);
                    ddlYear.SelectedValue = year;
                }

                DropDownList ddlMonth = e.Item.Cells[2].FindControl("edit_BirthMonth") as DropDownList;
                if (ddlMonth != null)
                {
                    Tools.Tools.FillComboMesi(ddlMonth);
                    ddlMonth.SelectedValue = month;
                }

                DropDownList ddlDay = e.Item.Cells[2].FindControl("edit_BirthDay") as DropDownList;
                if (ddlDay != null)
                {
                    Tools.Tools.FillComboDaysInMonthSelected(ddlDay, int.Parse(month), int.Parse(year));
                    if (ViewState["Month"] == null && ViewState["Year"] == null)
                        ddlDay.SelectedValue = dt.Day.ToString();
                    else
                        ddlDay.SelectedValue = "1";
                }

                //manage belt
                DropDownList ddlBelt = e.Item.Cells[3].FindControl("edit_Belt") as DropDownList;
                if (ddlBelt != null)
                {
                    belt = new BusinessObject.Cintura();
                    Tools.Tools.FillCombo(ddlBelt, belt.GetTbBelts(), "Id", "Belt");
                    if (ViewState["Belt"] == null)
                        ddlBelt.SelectedValue = (e.Item.DataItem as DataRowView).Row["Belt"].ToString();
                    else
                        ddlBelt.SelectedValue = ViewState["Belt"].ToString();
                }

                DropDownList ddlGrappling = e.Item.Cells[3].FindControl("ddl_Grappling") as DropDownList;
                if (ddlGrappling != null)
                {
                    Tools.Tools.FillGrapplingLevel(ddlGrappling);
                    if (ViewState["Grappling"] == null)
                        ddlGrappling.SelectedValue = (e.Item.DataItem as DataRowView).Row["GrapplingLevel"].ToString();
                    else
                        ddlGrappling.SelectedValue = ViewState["Grappling"].ToString();
                }
            }
        }

        protected void dtgAtleta_Update(object sender, DataGridCommandEventArgs e)
        {
            BusinessObject.Atleta a = new BusinessObject.Atleta();

            if (!Page.IsValid)
                return;

            a.Id = long.Parse(dtgAtleta.DataKeys[e.Item.ItemIndex].ToString());
            TextBox atleta = e.Item.FindControl("edit_Atleta") as TextBox;
            DropDownList gender = e.Item.FindControl("edit_Gender") as DropDownList;
            DropDownList birthYear = e.Item.FindControl("edit_BirthYear") as DropDownList;
            DropDownList birthMonth = e.Item.FindControl("edit_BirthMonth") as DropDownList;
            DropDownList birthDay = e.Item.FindControl("edit_BirthDay") as DropDownList;
            DropDownList belt = e.Item.FindControl("edit_Belt") as DropDownList;
            DropDownList grappling = e.Item.FindControl("ddl_Grappling") as DropDownList;
            TextBox matchFC = e.Item.FindControl("edit_MatchFullContact") as TextBox;
            TextBox matchLK = e.Item.FindControl("edit_MatchLowKick") as TextBox;
            TextBox matchKR = e.Item.FindControl("edit_MatchK1Rules") as TextBox;
            TextBox weight = e.Item.FindControl("edit_Weight") as TextBox;
            
            if (atleta != null)
                a.FullName = atleta.Text.Replace("'", "''");

            if (gender != null)
                a.Gender = int.Parse(gender.SelectedValue);

            if (birthYear != null && birthMonth != null && birthDay != null)
                a.BirthDayString = string.Format("{0}/{1}/{2}", birthYear.SelectedValue, birthMonth.SelectedValue, birthDay.SelectedValue);

            if (belt != null)
                a.Belt = int.Parse(belt.SelectedValue);

            if (grappling != null)
                a.GrapplingLevel = grappling.SelectedValue;

            if (matchFC != null)
                a.MatchFullContact = int.Parse(matchFC.Text);

            if (matchLK != null)
                a.MatchLowKick = int.Parse(matchLK.Text);

            if (matchKR != null)
                a.MatchK1Rules = int.Parse(matchKR.Text);

            if (weight != null)
                a.Weight = float.Parse(weight.Text);

            if (Session["EMail"] == null)
                Response.Redirect("Autenticazione.aspx");

            a.Update();

            dtgAtleta.EditItemIndex = -1;
            LoadGrid();
        }

        protected void dtgAtleta_SelectedIndexChanged(object sender, DataGridPageChangedEventArgs e)
        {
            ClearViewState();
            dtgAtleta.CurrentPageIndex = e.NewPageIndex;
            dtgAtleta.EditItemIndex = -1;
            LoadGrid();
        }

        protected void BirthYear_Changed(object sender, EventArgs e)
        {
            ViewState.Add("Year", (sender as DropDownList).SelectedValue);
            LoadGrid();
        }

        protected void BirthMonth_Changed(object sender, EventArgs e)
        {
            ViewState.Add("Month", (sender as DropDownList).SelectedValue);
            LoadGrid();
        }

        protected void Belt_Changed(object sender, EventArgs e)
        {
            ViewState.Add("Belt", (sender as DropDownList).SelectedValue);
            LoadGrid();
        }

        protected void Grappling_Changed(object sender, EventArgs e)
        {
            ViewState.Add("Grappling", (sender as DropDownList).SelectedValue);
            LoadGrid();
        }

        protected void ClearViewState()
        {
            ViewState.Remove("Year");
            ViewState.Remove("Month");
            ViewState.Remove("Belt");
            ViewState.Remove("Grappling");
        }
    }
}